<template>
    <div class="pack">
        <div class="tree_container">
            <h3>机构列表</h3>
            <div class="treePack">
                <Tree :data="organizationTreeData" @on-select-change="organizationTreeChange"></Tree>
            </div>
        </div>
        <div class="detail_container">
            <h3>应急物资储备点管理</h3>
            <div class="hunttool">
                <Input v-model="valueEsName" size="small" placeholder="存储点名称" class="hunt" ></Input>
                <Button type="primary" icon="ios-search" size="small"  @click="searchData">查询</Button>
                <Button type="ghost" icon="ios-trash-outline" size="small" class="deltebtn" @click="batchDelete">批量删除</Button>
                <Button type="ghost" icon="ios-plus-empty" size="small" class="addbtn" @click="addnew">新增</Button>
            </div>
            <div class="compile">
                <Table stripe  border :columns="columns" :data="tabledata" @on-selection-change="compileSelect"></Table>
            </div>
            <div class="page_container">
                <Page :current="pageNo" :total="totalCount" size="small" simple  class="page_btn" @on-change="pageChange"></Page>
                <P class="page_text">共{{totalCount}}条</P>
            </div>
        </div>
        <!--新增、修改弹出框-->
        <Modal v-model="compile" :title="compiletitle" :closable="false" :mask-closable="false">
            <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="100">
                <Form-item label="所属单位" prop="departmentName">
                    <p >{{formValidate.departmentName}}</p>
                </Form-item>
                <Form-item label="存储点名称" prop="setPlace">
                    <Input v-model="formValidate.setPlace" placeholder="请输入存储点名称"></Input>
                </Form-item>
                <Form-item label="地图编号" prop="mapNum">
                    <Input v-model="formValidate.mapNum" placeholder="请输入地图编号"></Input>
                </Form-item>
                <Form-item label="位置说明" prop="plDesc">
                    <Input v-model="formValidate.plDesc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" placeholder=""></Input>
                </Form-item>
            </Form>
            <div slot="footer">
                <Button type="ghost" @click="compileCancel()">取消</Button>
                <Button type="primary" @click="compileSubmit('formValidate')">提交</Button>
            </div>
        </Modal>
        <!--查看弹出框-->
        <Modal v-model="examine" title="查看物资储备点" :closable="false" :mask-closable="false">
            <Form ref="examineValidate" :model="examineValidate" :rules="ruleValidate" :label-width="100">
                <Form-item label="所属单位" prop="departmentName">
                    <p>{{examineValidate.departmentName}}</p>
                </Form-item>
                <Form-item label="存储点名称" prop="setPlace">
                    <p>{{examineValidate.setPlace}}</p>
                </Form-item>
                <Form-item label="地图编号" prop="mapNum">
                    <p>{{examineValidate.mapNum}}</p>
                </Form-item>
                <Form-item label="位置说明" prop="plDesc">
                    <Input v-model="examineValidate.plDesc" type="textarea" :autosize="{minRows: 2,maxRows: 5}" readonly placeholder=""></Input>
                </Form-item>
            </Form>
            <div slot="footer">
                <Button type="ghost" @click="examineCancel()">关闭</Button>
            </div>
        </Modal>
    </div>
</template>
<script type="text/javascript">
    var ezjsUtil = Vue.ezjsUtil;
    var apiConstants = ezjsUtil.constants.api;
    export default {
        components: {},
        data() {
            return {
                departmentCode:CURRENT_LEADER.departmentCode,  //登录人员code值
                valueLen:'', //点击树获得值的长度
                compile: false,
                compiletitle:"应急物资储备点管理",
                //搜索存放点
                valueEsName:'',
                //机构树列表数据
                organizationTreeData:[],
                //分页页数
                pageNo:1,
                //分页共多条记录
                totalCount:0,
                //批量删除ID
                compiledele:'',
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },
                    {
                        type: 'index',
                        width: 60,
                        align: 'center'
                    },
                    {
                        title: '所属单位',
                        key: 'departmentName',
                    },
                    {
                        title: '存储点名称',
                        key: 'setPlace'
                    },
                    {
                        title: '地图编号',
                        key: 'mapNum'
                    },
                    {
                        title: '位置说明',
                        key: 'plDesc'
                    },
                    {
                        title: '编辑',
                        key: 'action',
                        width: 150,
                        align: 'center',
                        render: (h, params) => {
                            return h('div', [
                                h('Button', {
                                    props: {
                                        type: 'primary',
                                        size: 'small'
                                    },
                                    style: {
                                        marginRight: '5px'
                                    },
                                    on: {
                                        click: () => {
                                            this.examine=true;
                                            this.examineValidate=params.row;
                                        }
                                    }
                                }, '查看'),
                                h('Button', {
                                    props: {
                                        type: 'error',
                                        size: 'small'
                                    },
                                    on: {
                                        click: () => {
                                            this.supplieStorageRemoveByIds(params.row.id);
                                        }
                                    }
                                }, '删除')
                            ]);
                        }
                    }
                ],

                tabledata: [],
                //新增修改表单
                formValidate: {
                    id:'',
                    departmentCode: '',
                    departmentName: '',
                    setPlace: '',
                    plDesc: '',
                    mapNum:'',
                },
                ruleValidate: {
                    name: [
                        { required: true, message: '请选择组织机构', trigger: 'blur' }
                    ],
                    setPlace: [
                        { required: true, message: '请填写存储点名称', trigger: 'blur' }
                    ],
                    mpNum: [
                        { required: false, message: '请填写地图编号', trigger: 'blur' }
                    ],
/*                    plDesc: [
                        { required: false, message: '请填写位置说明', trigger: 'blur' },
                        { type: 'string', min: 20, message: '介绍不能少于20字', trigger: 'blur' }
                    ]*/
                },
                //查看表单
                examine:false,
                examineValidate:{
                    departmentName:'',
                    setPlace:'',
                    mapNum:'',
                    plDesc:'',
                },
            }
        },
        computed: {},
        created() {
            this.supplieStorageFlipList();
            this.organizationFlipList();
        },
        methods: {
            //提示信息
            instance () {
                const title = '提示';
                const content = '<p>请在机构列表选择机构单位！</p>';
                this.$Modal.info({
                    title: title,
                    content: content
                });
            },
            //机构树点击事件
            organizationTreeChange(val){
                this.valueLen = val.length;
                if(val.length>0   && this.departmentCode<=val[0].code){
                    this.formValidate.departmentName=val[0].fullTitle;
                    this.formValidate.departmentCode=val[0].code;
                }else {
                    this.formValidate.departmentCode='';
                    this.formValidate.departmentName='';
                }
                this.searchData();
            },
            //搜索
            searchData(){
                this.pageNo=1;
                this.supplieStorageFlipList();
            },
            //新增按钮
            addnew(){
                if(this.formValidate.departmentName==""){
                    if( this.valueLen <= 0){
                        this.instance ();
                    }else{
                        this.$Message.error('权限不够，无法新增，请联系管理员！');
                    }
                }else {
                    this.formValidate.id="";
                    this.compile = true;
                }
            },
            //取消
            compileCancel(){
                if( this.formValidate.id !==""){
                    this.compileReset('formValidate');
                    this.organizationFlipList();
                }
                this.compile=false;
            },
            //分页改变时
            pageChange(val){
                this.pageNo=val;
                this.supplieStorageFlipList();
            },
            //表单重置
            compileReset (name) {
                this.$refs[name].resetFields();
            },
            //表格勾选
            compileSelect(selection,row){
                var ids=[];
                for (var i=0;i<selection.length;i++){
                    ids.push(selection[i].id);
                }
                this.compiledele=ids.join(',');
            },
            //批量删除
            batchDelete(){
                if(this.compiledele) {
                    this.supplieStorageRemoveByIds(this.compiledele);
                }else {
                    this.$Message.error('请选择删除项!');
                }
            },
            //新增或修改存储点
            compileSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        var _this = this;
                        ezjsUtil.request(apiConstants.supplieStorage_edit,
                            {
                                id:_this.formValidate.id,
                                departmentCode: _this.formValidate.departmentCode,
                                departmentName: _this.formValidate.departmentName,
                                setPlace: _this.formValidate.setPlace,
                                plDesc: _this.formValidate.plDesc,
                                mapNum: _this.formValidate.mapNum,
                            },
                            function (err, data) {
                                if (err) {
                                    return;
                                }
                                _this.organizationFlipList();
                                _this.formValidate.departmentCode="";
                                _this.pageNo=1;
                                _this.valueLen='';
                                _this.supplieStorageFlipList();
                            }
                        )
                        _this.$Message.success('提交成功!');
                        _this.compile=false;
                        _this.compileReset('formValidate');
                    } else {
                        this.$Message.error('表单验证失败!');
                    }
                })
            },
            examineCancel(){
                this.examine=false;
            },
            //查询存储点列表
            supplieStorageFlipList(){
                var _this = this;
                ezjsUtil.request(apiConstants.supplieStorage_flipList,
                    {
                        departmentCode:_this.formValidate.departmentCode,
                        setPlace:_this.valueEsName,
                        pageNo:_this.pageNo
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.tabledata=data.listInfo;
                        _this.totalCount=data.flipInfo.totalCount;
//                        _this.RecordPage.totalPage=data.flipInfo.totalPage;
                    }
                )
            },

            //批量删除物资登记
            supplieStorageRemoveByIds(delid){
                var _this = this;
                ezjsUtil.request(apiConstants.supplieStorage_removeByIds,
                    {
                        ids:delid
                    },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.compiledele='';
                        _this.supplieStorageFlipList();
                    }
                )
            },
            //获取所有组织机构树
            organizationFlipList() {
                var _this = this;
                ezjsUtil.request(apiConstants.organization_flipList,
                    {  },
                    function (err, data) {
                        if (err) {
                            return;
                        }
                        _this.organizationTreeData=data;
                        _this.organizationTreeData[0].expand=true;
                    }
                )
            },
        },
        mounted() {
        }
    }
</script>
<style lang="less" scoped>
    .pack{
        width: 100%;
        height: 100%;
    }
    .tree_container{
        width: 260px;
        height: 100%;
        float: left;
        border: 1px solid #d3d8e0;
    h3{
        line-height: 40px;
        text-align: center;
        font-size: 14px;
        color: #6e6d6d;
    }
    }
    .detail_container{
        height: 100%;
        margin-left: 280px;
        overflow: hidden;
        overflow-y: auto;
    }
    h3 {
        height: 40px;
        line-height: 40px;
        border-bottom: 1px solid #d3d8e0;
    }
    .treePack{
        height: 94%;
        padding: 10px;
        overflow: auto;
    }
    .hunttool {
        padding:15px 0;
    .addbtn {
        margin-right: 6px;
        float: right;
    }

    .deltebtn {
        margin-right: 6px;
        float: right;
    }

    }
    .hunt {
        width: 14rem;
    }
    .page_container{
        height: 30px;
        margin-top: 20px;
    .page_btn{
        float: right;
    }
    .page_text{
        float: right;
        line-height: 26px;
    }
    }
</style>
